import { useUserStore } from '@/stores'
import { createRouter, createWebHistory } from 'vue-router'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    { path: '/login', component: () => import('@/views/LoginView.vue') },
    {
      path: '/home',
      component: () => import('@/views/HomeView.vue'),
      redirect: '/welcome',
      children: [
        { path: '/welcome', component: () => import ('@/views/WelcomeView.vue') },
        { path: '/users', component: () => import('@/views/user/UserView.vue') },
        { path: '/rights', component: () => import('@/views/rightsRole/RightsView.vue') },
        { path: '/roles', component: () => import('@/views/rightsRole/RolesView.vue') },
        { path: '/categories', component: () => import('@/views/goods/CategoriesView.vue') },
      ],
    },
  ],
})

router.beforeEach((to) => {
  const userStore = useUserStore()
  if (to.path !== '/login' && !userStore.token) {
    return '/login'
  }
})
export default router
